﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Drawing2D;
using System.Drawing;

namespace Maydew.Windows.Forms.Controls.FlexiCell
{
	public partial class FlexiCellControl
    {
        #region " 背景色 "
        /// <summary>
        /// 设置选中单元格的背景色。
        /// </summary>
        /// <param name="color">颜色。</param>
        public void SetBackColor(Color color)
        {
            int srow, erow;
            string scol, ecol;
            GetCellRectangle(_focusedCells, out srow, out scol, out erow, out ecol);
            SetBackColor(srow, scol, erow, ecol, color);
        }
        /// <summary>
        /// 设置指定单元格的背景色。
        /// </summary>
        /// <param name="startRowIndex">开始行。</param>
        /// <param name="startColumnName">开始列。</param>
        /// <param name="endRowIndex">结束行。</param>
        /// <param name="endColumnName">结束列。</param>
        /// <param name="color">颜色。</param>
        public void SetBackColor(int startRowIndex, string startColumnName, int endRowIndex, string endColumnName,Color color)
        {
            int startColumnIndex = _columns.IndexOf(startColumnName);
            int endColumnIndex = _columns.IndexOf(endColumnName);
            for (int r = startRowIndex; r <= endRowIndex; r++)
            {
                for (int c = startColumnIndex; c <= endColumnIndex; c++)
                {
                    Cell cell = _cells[r, _columns[c].ColumnName];
                    cell.BackColor = color;
                }
            }
            base.Refresh();
        }
        #endregion

        #region " 前景色 "
        /// <summary>
        /// 设置选中单元格的前景色。
        /// </summary>
        /// <param name="color">颜色。</param>
        public void SetForeColor(Color color)
        {
            int srow, erow;
            string scol, ecol;
            GetCellRectangle(_focusedCells, out srow, out scol, out erow, out ecol);
            SetForeColor(srow, scol, erow, ecol, color);
        }
        /// <summary>
        /// 设置指定单元格的前景色。
        /// </summary>
        /// <param name="startRowIndex">开始行。</param>
        /// <param name="startColumnName">开始列。</param>
        /// <param name="endRowIndex">结束行。</param>
        /// <param name="endColumnName">结束列。</param>
        /// <param name="color">颜色。</param>
        public void SetForeColor(int startRowIndex, string startColumnName, int endRowIndex, string endColumnName, Color color)
        {
            int startColumnIndex = _columns.IndexOf(startColumnName);
            int endColumnIndex = _columns.IndexOf(endColumnName);
            for (int r = startRowIndex; r <= endRowIndex; r++)
            {
                for (int c = startColumnIndex; c <= endColumnIndex; c++)
                {
                    Cell cell = _cells[r, _columns[c].ColumnName];
                    cell.ForeColor = color;
                }
            }
            base.Refresh();
        }
        #endregion

        #region " 字体 "
        /// <summary>
        /// 设置选中单元格的字体。
        /// </summary>
        /// <param name="font">字体。</param>
        public void SetFont(Font font)
        {
            int srow, erow;
            string scol, ecol;
            GetCellRectangle(_focusedCells, out srow, out scol, out erow, out ecol);
            SetFont(srow, scol, erow, ecol, font);
        }
        /// <summary>
        /// 设置指定单元格的字体。
        /// </summary>
        /// <param name="startRowIndex">开始行。</param>
        /// <param name="startColumnName">开始列。</param>
        /// <param name="endRowIndex">结束行。</param>
        /// <param name="endColumnName">结束列。</param>
        /// <param name="font">字体。</param>
        public void SetFont(int startRowIndex, string startColumnName, int endRowIndex, string endColumnName, Font font)
        {
            int startColumnIndex = _columns.IndexOf(startColumnName);
            int endColumnIndex = _columns.IndexOf(endColumnName);
            for (int r = startRowIndex; r <= endRowIndex; r++)
            {
                for (int c = startColumnIndex; c <= endColumnIndex; c++)
                {
                    Cell cell = _cells[r, _columns[c].ColumnName];
                    cell.Font = font;
                }
            }
            base.Refresh();
        }
        #endregion

        #region " 对齐样式 "
        /// <summary>
        /// 设置选中单元格的对齐样式。
        /// </summary>
        /// <param name="align">对齐样式。</param>
        public void SetTextAlign(ContentAlignment align)
        {
            int srow, erow;
            string scol, ecol;
            GetCellRectangle(_focusedCells, out srow, out scol, out erow, out ecol);
            SetTextAlign(srow, scol, erow, ecol, align);
        }
        /// <summary>
        /// 设置指定单元格的对齐样式。
        /// </summary>
        /// <param name="startRowIndex">开始行。</param>
        /// <param name="startColumnName">开始列。</param>
        /// <param name="endRowIndex">结束行。</param>
        /// <param name="endColumnName">结束列。</param>
        /// <param name="align">对齐样式。</param>
        public void SetTextAlign(int startRowIndex, string startColumnName, int endRowIndex, string endColumnName, ContentAlignment align)
        {
            int startColumnIndex = _columns.IndexOf(startColumnName);
            int endColumnIndex = _columns.IndexOf(endColumnName);
            for (int r = startRowIndex; r <= endRowIndex; r++)
            {
                for (int c = startColumnIndex; c <= endColumnIndex; c++)
                {
                    Cell cell = _cells[r, _columns[c].ColumnName];
                    cell.TextAlign = align;
                }
            }
            base.Refresh();
        }
        #endregion

        #region " 格式化 "
        /// <summary>
        /// 设置选中单元格的格式化样式。
        /// </summary>
        /// <param name="format">格式化样式。</param>
        public void SetFormat(string format)
        {
            int srow, erow;
            string scol, ecol;
            GetCellRectangle(_focusedCells, out srow, out scol, out erow, out ecol);
            SetFormat(srow, scol, erow, ecol, format);
        }
        /// <summary>
        /// 设置指定单元格的格式化样式。
        /// </summary>
        /// <param name="startRowIndex">开始行。</param>
        /// <param name="startColumnName">开始列。</param>
        /// <param name="endRowIndex">结束行。</param>
        /// <param name="endColumnName">结束列。</param>
        /// <param name="format">格式化样式。</param>
        public void SetFormat(int startRowIndex, string startColumnName, int endRowIndex, string endColumnName, string format)
        {
            int startColumnIndex = _columns.IndexOf(startColumnName);
            int endColumnIndex = _columns.IndexOf(endColumnName);
            for (int r = startRowIndex; r <= endRowIndex; r++)
            {
                for (int c = startColumnIndex; c <= endColumnIndex; c++)
                {
                    Cell cell = _cells[r, _columns[c].ColumnName];
                    cell.Format = format;
                }
            }
            base.Refresh();
        }
        #endregion

        #region " 编辑器 "
        /// <summary>
        /// 设置选中单元格编辑器。
        /// </summary>
        /// <param name="editor">编辑器。</param>
        public void SetEditor(CellEditor editor)
        {
            int srow, erow;
            string scol, ecol;
            GetCellRectangle(_focusedCells, out srow, out scol, out erow, out ecol);
            SetEditor(srow, scol, erow, ecol, editor);
        }
        /// <summary>
        /// 设置指定单元格编辑器。
        /// </summary>
        /// <param name="startRowIndex">开始行。</param>
        /// <param name="startColumnName">开始列。</param>
        /// <param name="endRowIndex">结束行。</param>
        /// <param name="endColumnName">结束列。</param>
        /// <param name="editor">编辑器。</param>
        public void SetEditor(int startRowIndex, string startColumnName, int endRowIndex, string endColumnName, CellEditor editor)
        {
            int startColumnIndex = _columns.IndexOf(startColumnName);
            int endColumnIndex = _columns.IndexOf(endColumnName);
            for (int r = startRowIndex; r <= endRowIndex; r++)
            {
                for (int c = startColumnIndex; c <= endColumnIndex; c++)
                {
                    Cell cell = _cells[r, _columns[c].ColumnName];
                    cell.Editor = editor.Clone() as CellEditor;
                    cell.Value = null;
                }
            }
            base.Refresh();
        }
        #endregion
    }
}
